<?php
/**
 * 阿里云 mts
 * @author lmy
 * @version 1.0
 */

namespace App\Common\Comp;

use AlibabaCloud\SDK\Mts\V20140618\Models\QueryJobListRequest;
use AlibabaCloud\SDK\Mts\V20140618\Models\SubmitJobsRequest;
use AlibabaCloud\SDK\Mts\V20140618\Mts;
use Darabonba\OpenApi\Models\Config;
use Illuminate\Support\Facades\Log;

class VideoTranscodingComp
{
    const TRANSCODING = 1; //视频转码
    const WATERMARK   = 2; //视频加水印

    /**
     * SK初始化账号Client
     * @return Mts
     */
    public static function createClient()
    {
        $clientData = config('BaseConfig');
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $clientData['oss']['accessKeyId'],
            // 您的AccessKey Secret
            "accessKeySecret" => $clientData['oss']['accessKeySecret']
        ]);
        // 访问的域名
        $config->endpoint = $clientData['mts']['endpoint'];
        return new Mts($config);
    }

    /**
     * 调用视频处理 转码|加水印
     * @param $account
     * @param $path
     * @param $widthHeight
     * @param $type 1转码 2加水印
     * @return array
     */
    public function videoTranscodeAndWatermark($account, $path, $widthHeight, $type)
    {
        $client = self::createClient();
        $configData = config('BaseConfig');
        $videoPath = explode('.', $path);
        $outputObject = $videoPath[0];
        $tip = 'TRANSCODING';//转码
        if ($type == self::WATERMARK) {
            //加水印(可下载)
            $outputObject .= '-opt' . '.' . $videoPath[1];
            //加水印(可下载)
            $tip = 'WATERMARK';
        }
        $location = $configData['oss']['region'];
        $bucket = $configData['oss']['bucket'];
        $ossUrl = $configData['oss']['ossUrl'];
        $watermarkLogo = $configData['mts']['watermarkLogo'];
        $waterMarkTemplateId = $configData['mts']['waterMarkTemplateId'];
        $wordWaterMarkTemplateId = $configData['mts']['wordWaterMarkTemplateId'];
        $widthTailUrl = $ossUrl . $configData['mts']['widthTailUrl'];
        $heightTailUrl = $ossUrl . $configData['mts']['heightTailUrl'];
        $widthTemplateId = $configData['mts']['widthM3u8TemplateId'];
        $heightTemplateId = $configData['mts']['heightM3u8TemplateId'];
        if ($type == self::WATERMARK) {
            //加水印(可下载)
            $widthTemplateId = $configData['mts']['widthTemplateId'];
            $heightTemplateId = $configData['mts']['heightTemplateId'];
        }
        $pipelineId = $configData['mts']['pipelineId'];

        $templateId = $heightTemplateId;
        $tailUrl = $heightTailUrl;
        if ((int)$widthHeight[0] > (int)$widthHeight[1]) {
            //横屏
            $templateId = $widthTemplateId;
            $tailUrl = $widthTailUrl;
        }
        //转码参数
        $outputs = [[
            'OutputObject' => $outputObject,
            'TemplateId' => $templateId,
            'UserData' => $account
        ]];
        if ($type == self::WATERMARK) {
            //水印参数
            $outputs[0]['WaterMarks'] = [
                [
                    'InputFile' => [
                        'Bucket' => $bucket,
                        'Location' => $location,
                        'Object' => $watermarkLogo,//logo文件
                    ],
                    'WaterMarkTemplateId' => $waterMarkTemplateId//logo水印模板
                ],
                [
                    'InputFile' => [
                        'WaterMarkTemplateId' => $wordWaterMarkTemplateId,//文字水印模板
                    ],
                    'Type' => 'Text',
                    'ReferPos' => 'BottomLeft',
                    'FontColor' => 'FZHei-B01S',
                    'TextWaterMark' => json_encode(['Content' => base64_encode('纸鸢号: ' . $account), 'FontName' => 'FZHei-B01S',
                        'Top' => 82, 'Left' => 10, 'FontSize' => 36, 'FontAlpha' => 0.9, 'FontColor' => '#ffffff'
                    ]),
                ]
            ];
            $outputs[0]['TailSlateList'] = [
                [
                    'TailUrl' => $tailUrl, 'BgColor' => '#000000', 'Width' => 'full', 'Height' => 'full'
                ]
            ];
        }

        $input = [
            'Bucket' => $bucket,
            'Location' => $location,
            'Object' => $path,
        ];
        $req = [
            'input' => json_encode($input),
            'outputs' => json_encode($outputs),
            'outputBucket' => $bucket,
            'outputLocation' => $location,
            'pipelineId' => $pipelineId,
        ];
        Log::info('VIDEO_MST_' . $tip . '_REQUEST_请求参数:' . json_encode($req, JSON_UNESCAPED_UNICODE));
        $submitJobsRequest = new SubmitJobsRequest($req);
        $mtsInfo = $client->submitJobs($submitJobsRequest);
        Log::info('VIDEO_MST_' . $tip . '_REQUEST_请求结果:' . json_encode($mtsInfo, JSON_UNESCAPED_UNICODE));
        $data = [
            'mtsInfo' => $mtsInfo,
            'videoKey' => $outputObject,
            'log' => $req,
        ];

        return $data;
    }

    /**
     * 查询媒体转码|加水印作业详情
     * @param $jobId
     * @return \AlibabaCloud\SDK\Mts\V20140618\Models\QueryJobListResponse
     */
    public function videoTranscodeResult($jobId)
    {
        $client = self::createClient();
        $jobArr['jobIds'] = $jobId;
//        Log::info('VIDEO_MST_CHECK_REQUEST_请求参数:' . json_encode($jobArr, JSON_UNESCAPED_UNICODE));
        $queryJobListRequest = new QueryJobListRequest($jobArr);
        $data = $client->queryJobList($queryJobListRequest);
//        Log::info('VIDEO_MST_CHECK_REQUEST_请求结果:' . json_encode($data, JSON_UNESCAPED_UNICODE));
        return $data;
    }

}
